//全局对象
var exhibitionhall2;//2层展厅信息

var robot_list;//机器人列表

var unselect_robot_list;//用户未选择的机器人列表

var admin_list;//预约人列表

var industry_list;//行业列表

var order_list;//编辑状态下的预约列表

var quanxian_list;//根据选择人员展示权限管理列表

var responsibleperson_list;//配置管理负责人列表

var robotImage_list;//机器人形象库

var exhibitionhall_mapList;//展厅地图列表

var selectRow;//被选中的机器人形象

var selectMapRow;//被选中的场馆地图

var plate_list;//板块配置

var pannel_list;//展板配置

var editRow;//配置管理→业务配置→板块及展板配置→板块配置→点击编辑按钮

var editRow_pannel;//配置管理→业务配置→板块及展板配置→展板配置→点击编辑按钮

var robot_list_gl;//配置管理→业务配置→机器人名称配置--列表

var jiangjie_list;//讲解词列表

var emoji_list;//表情列表

var ad_list;//广告列表

var AD_file;//广告文件

var adRules_list;//广告规则

var point_list;//机器人路线点位列表

var log_list;//服务器日志列表

var level_list;//告警配置→告警信息级别配置列表

// videojs zh-CN.json 中文包
var videojsLang = {
	"Play": "播放",
	"Pause": "暂停",
	"Current Time": "当前时间",
	"Duration": "时长",
	"Remaining Time": "剩余时间",
	"Stream Type": "媒体流类型",
	"LIVE": "直播",
	"Loaded": "加载完毕",
	"Progress": "进度",
	"Fullscreen": "全屏",
	"Non-Fullscreen": "退出全屏",
	"Mute": "静音",
	"Unmute": "取消静音",
	"Playback Rate": "播放速度",
	"Subtitles": "字幕",
	"subtitles off": "关闭字幕",
	"Captions": "内嵌字幕",
	"captions off": "关闭内嵌字幕",
	"Chapters": "节目段落",
	"Close Modal Dialog": "关闭弹窗",
	"Descriptions": "描述",
	"descriptions off": "关闭描述",
	"Audio Track": "音轨",
	"You aborted the media playback": "视频播放被终止",
	"A network error caused the media download to fail part-way.": "网络错误导致视频下载中途失败。",
	"The media could not be loaded, either because the server or network failed or because the format is not supported.": "视频因格式不支持或者服务器或网络的问题无法加载。",
	"The media playback was aborted due to a corruption problem or because the media used features your browser did not support.": "由于视频文件损坏或是该视频使用了你的浏览器不支持的功能，播放终止。",
	"No compatible source was found for this media.": "无法找到此视频兼容的源。",
	"The media is encrypted and we do not have the keys to decrypt it.": "视频已加密，无法解密。",
	"Play Video": "播放视频",
	"Close": "关闭",
	"Modal Window": "弹窗",
	"This is a modal window": "这是一个弹窗",
	"This modal can be closed by pressing the Escape key or activating the close button.": "可以按ESC按键或启用关闭按钮来关闭此弹窗。",
	", opens captions settings dialog": ", 开启标题设置弹窗",
	", opens subtitles settings dialog": ", 开启字幕设置弹窗",
	", opens descriptions settings dialog": ", 开启描述设置弹窗",
	", selected": ", 选择",
	"captions settings": "字幕设定",
	"Audio Player": "音频播放器",
	"Video Player": "视频播放器",
	"Replay": "重播",
	"Progress Bar": "进度小节",
	"Volume Level": "音量",
	"Close Modal Dialog": "关闭弹窗",
	"subtitles settings": "字幕设定",
	"descriptions settings": "描述设定",
	"Text": "文字",
	"White": "白",
	"Black": "黑",
	"Red": "红",
	"Green": "绿",
	"Blue": "蓝",
	"Yellow": "黄",
	"Magenta": "紫红",
	"Cyan": "青",
	"Background": "背景",
	"Window": "视窗",
	"Transparent": "透明",
	"Semi-Transparent": "半透明",
	"Opaque": "不透明",
	"Font Size": "字体尺寸",
	"Text Edge Style": "字体边缘样式",
	"None": "无",
	"Raised": "浮雕",
	"Depressed": "压低",
	"Uniform": "均匀",
	"Dropshadow": "下阴影",
	"Font Family": "字体库",
	"Proportional Sans-Serif": "比例无细体",
	"Monospace Sans-Serif": "单间隔无细体",
	"Proportional Serif": "比例细体",
	"Monospace Serif": "单间隔细体",
	"Casual": "舒适",
	"Script": "手写体",
	"Small Caps": "小型大写字体",
	"Reset": "重启",
	"restore all settings to the default values": "恢复全部设定至预设值",
	"Done": "完成",
	"Caption Settings Dialog": "字幕设定视窗",
	"Beginning of dialog window. Escape will cancel and close the window.": "开始对话视窗。离开会取消及关闭视窗",
	"End of dialog window.": "结束对话视窗"
  }

$(function () {

	divChange.videoChange();
	//将所有时间控件的默认选定时间设定为现在
	$('.datetimepicker.now').datetimepicker('update', date());
	$('.datetimepickerMonth').datetimepicker('update', date());
	$('.datetimepickerDay').datetimepicker('update', date());

	//行内编辑defaults配置
	$.fn.editable.defaults.mode = 'inline';

	/*  var bg = new Image();
		bg.src = "img/f2.jpg";*/

	//initCanvas(bg);



	exhibitionhallList();//展厅列表

	setTimeout(robotsList(), 500); //机器人列表
	setTimeout(adminuser(), 1000); //预约人列表

	setTimeout(industryList(), 1500);//2.4.3.1	获取行业列表接口



	purviewRoleList();//获取配置权限（角色列表）



});

/*首页↓*/

var div = document.getElementById('homeZoom');
//var test = document.getElementById('testText');
var canvas = document.getElementById('robotPosition');
var ctx = canvas.getContext('2d');
var pic;
var divWidth;
var divHeight;
var canvasOffsetX; //canvas距离浏览器边缘的位置
var canvasOffsetY;
var canvasWidth;
var canvasHeight;
var canvasX;
var canvasY;
var finger;
var offsetX; //单指移动图片相对div在X轴上的偏移
var offsetY; //单指移动图片相对div在Y轴上的偏移
var singleX1; //单指起始X轴位置
var singleY1; //单指起始Y轴位置
var singleX2; //单指移动X轴位置
var singleY2; //单指移动Y轴位置
var dis1; //双指缩放初始两指间的距离
var dis2; //双指缩放触摸结束时两指间的距离
var centerX; //双指缩放X轴的中心位置
var centerY; //双指缩放Y轴的中心位置
var picX; //双指缩放中心位置在原始大小图片的X轴位置
var picY; //双指缩放中心位置在原始大小图片的Y轴位置
var zoom; //缩放系数
var newZoom; //变化后的缩放系数

window.onload = function () {
	initImg();

	// ajaxRobotPosition()


}
// function ajaxRobotPosition() {
// 	$.ajax({
// 		type: "post",
// 		url: "http://192.168.1.126:8080/lw_robot/h5/api/robot/running/control/video/open",
// 		async: false,
// 		dataType: 'json',
// 		error: function (xhr) {
// 			alert('無法取得資料');
// 		},
// 		success: function (data) {
// 			console.log('成功');
// 			//    console.log(JSON.stringify(data, null, 2));
// 			for (var i = 0; i < data.length; i++) {
// 				initRobot(data[i].robot, data[i].point);
// 			}
// 		}
// 	});
// }
window.onresize = function () {
	initImg();


}
//获取展厅列表
function exhibitionhallList() {
	getExhibitionhallList();
}

//获取机器人
function robotsList() {
	getRobotsList();
}
//获取预约人
function adminuser() {
	getAdminuserList();
}
//获取行业列表
function industryList() {
	getIndustryList();
}
canvas.addEventListener('touchstart', function (e) {
	if (e.targetTouches.length == 1) {
		//					return
		singleX1 = e.touches[0].clientX;
		singleY1 = e.touches[0].clientY;
		//		test.innerHTML = 'singleX1:' + singleX1 + ',singleY1:' + singleY1;
	} else {
		var x1 = e.touches[1].clientX - e.touches[0].clientX;
		var y1 = e.touches[1].clientY - e.touches[0].clientY;
		dis1 = Math.sqrt(x1 * x1 + y1 * y1);
		//得到两指中间点的坐标
		centerX = (e.touches[1].clientX + e.touches[0].clientX) / 2;
		centerY = (e.touches[1].clientY + e.touches[0].clientY) / 2;
		//两指中点在原始图片上的坐标
		picX = centerX - canvasOffsetX + canvasX;
		picY = centerY - canvasOffsetY + canvasY;
		//					test.innerHTML = 'centerX:' + centerX + ',centerY:' + centerY ;
		//					alert('x1:' + x1 + ',y1:' + y1 + ',dis1:' + dis1);
	}
}, false)

canvas.addEventListener('touchmove', function (e) {
	if (e.targetTouches.length == 1) {
		if (zoom < 0.4) {
			//			test.innerHTML = 'zoom太小了';
		} else {
			singleX2 = e.touches[0].clientX;
			singleY2 = e.touches[0].clientY;
			var offsetX = (singleX2 - singleX1) * 0.05;
			var offsetY = (singleY2 - singleY1) * 0.05;
			canvasX = canvasX + offsetX;
			canvasY = canvasY + offsetY;
			var maxOffsetX = -(pic.width * zoom - divWidth);
			var maxOffsetY = -(pic.height * zoom - divHeight);
			//图片的X轴偏移不超出div
			if (canvasX > 0) {
				canvasX = 0;
			} else if (canvasX < maxOffsetX) {
				canvasX = maxOffsetX;
			}
			//图片的Y轴偏移不超出div
			if (canvasY > 0) {
				//							test.innerHTML = 'canvasY > 0';
				canvasY = 0;
			} else if (canvasY < maxOffsetY) {
				canvasY = maxOffsetY;
			}
			//						test.innerHTML = 'canvasX:' + canvasX + ',canvasY:' + canvasY;
			ctx.clearRect(0, 0, divWidth, divHeight);
			ctx.drawImage(pic, canvasX, canvasY, canvasWidth, canvasHeight);
		}
	} else {
		var x2 = e.touches[1].clientX - e.touches[0].clientX;
		var y2 = e.touches[1].clientY - e.touches[0].clientY;
		dis2 = Math.sqrt(x2 * x2 + y2 * y2);
		disChange = dis2 - dis1;
		newZoom = zoom + disChange * 0.001;
		if (newZoom > 4) {
			newZoom = 4;
		} else if (newZoom < 0.4) {
			//			test.innerHTML = '太小了';
			newZoom = 0.4;
		}
		//					test.innerHTML = newZoom;
		ctx.clearRect(0, 0, divWidth, divHeight);
		canvasWidth = pic.width * newZoom;
		canvasHeight = pic.height * newZoom;
		canvasX = picX * newZoom - centerX;
		canvasY = picY * newZoom - centerY;
		ctx.drawImage(pic, canvasX, canvasY, canvasWidth, canvasHeight);
		zoom = newZoom;
		//		test.innerHTML = 'zoom:' + newZoom;
	}
}, false)

//初始化img图片
function initImg() {
	var rect = canvas.getBoundingClientRect();
	divWidth = div.offsetWidth;
	divHeight = div.offsetHeight;
	var divRatio = divWidth / divHeight;
	pic = new Image();
	pic.src = "img/floor2.jpg";
	if (!canvas.getContext) {
		return;
	}
	canvas.width = divWidth;
	canvas.height = divHeight;
	canvasX = 0;
	canvasY = 0;
	pic.onload = function (e) {
		e.stopPropagation();
		var picRatio = pic.width / pic.height;
		if (picRatio >= divRatio) {
			canvasWidth = divWidth;
			canvasHeight = divWidth / picRatio;
		} else {
			canvasWidth = (divHeight * picRatio);
			canvasHeight = divHeight;
		}
		zoom = canvasWidth / pic.width;
		ctx.drawImage(pic, canvasX, canvasY, canvasWidth, canvasHeight);
		canvasOffsetX = $('#robotPosition').offset().left;
		canvasOffsetY = $('#robotPosition').offset().top;
		//		test.innerHTML = 'canvasOffsetX:' + canvasOffsetX + ',canvasOffsetY:' + canvasOffsetY;
	}
}



function robotPosition(point) {
	//传入点位，比如点位1、点位2，返回该点位在原始图片上的坐标
	var pt = new Object();
	if (point = 1) {  //迎宾位
		pt.X = 1500;
		pt.Y = 1100;
	} else if (point = 2) {  //播放位
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 3) {  //智慧城市1
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 4) {  //智慧城市2
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 5) {  //核心技术区
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 6) {  //城市通
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 7) {  //蜻蜓停车1
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 8) {  //蜻蜓停车2
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 9) {  //小电卫士
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 10) {  //政务1
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 11) {  //政务2
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 12) {  //政务3
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 13) {  //政法
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 14) {  //公安
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 15) {  //大数据
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 16) {  //水平台
		pt.X = 100;
		pt.Y = 200;
	} else if (point = 17) {  //水科
		pt.X = 100;
		pt.Y = 200;
	}
	return pt;
}

/*var canvas = document.getElementById("robotPosition");
var ctx = canvas.getContext("2d");
var canvasWidth = $(".home > .top").width();
var canvasHeight = $(".home > .top").height();

function initCanvas(bg){
	if(!canvas.getContext) return;
	canvas.width = canvasWidth;
	canvas.height = canvasHeight;
	bg.onload = function() {
		ctx.drawImage(bg, 0, 0, canvasWidth, canvasHeight);
		var robot = new Image();
		robot.src = "img/u88.png";
		robot.onload = function() {
			ctx.drawImage(robot, 200, 100);
			ctx.drawImage(robot, 400, 300);
		}
	}
}

function drawBg(bg,bgX,bgY,bgWidth,bgHeight, fn){
	bg.onload = function() {
		ctx.drawImage(bg, x, y, width, height);
		drawRobot();
	}
}

function drawRobot(robot,x,y){
	
	if(!canvas.getContext) return;
	robot.onload = function() {
		ctx.drawImage(robot, x, y);
	}
}*/


/*首页↑*/

/*实时监控↓*/

// 实时监控，视频播放
function initRobotVideo(){
	var robotId = $('.control .pickRobot option:selected').val() || 1;//机器人ID 默认小威
	var videoUrl = api.video_url + robotId + '/index.m3u8';
	// videoUrl = './js/b.m3u8'
	var options = {languages:{
		'zh-CN':videojsLang
	}};

	if(window.videoPlayer){
		window.videoPlayer.src(videoUrl)
		window.videoPlayer.load(videoUrl);
		window.videoPlayer.play();
	}else{
		window.videoPlayer = videojs('robotVideo', options, function onPlayerReady() {
			videojs.log('Your player is ready!');
			this.src(videoUrl);
			this.load(videoUrl);
			this.play();
	
			// How about an event listener?
			this.on('ended', function () {
				videojs.log('Awww...over so soon?!');
			});
		});
	}

	
}

/**
 * 实时监控，图片拖动
 * @param {String} scrollId 滚动块id
 * @param {String} picId 图片id
 */
function picDrag(scrollId, picId){
	
	var dragX=-1;
	var dragY=-1;
	var $scrollDom = $(scrollId);
	$(picId).bind({
		mouseout:function(){
			dragX=-1;
		},
		mousemove:function(e){
			if(dragX>=0){
				var newX = e.pageX || (e.clientX );
				var newY = e.pageY || (e.clientY );
				$scrollDom.scrollLeft($scrollDom.scrollLeft()+(dragX-newX));
				$scrollDom.scrollTop($scrollDom.scrollTop()+(dragY-newY));
				dragX = newX;
				dragY = newY;
			}
		},
		mousedown:function(e){
			dragX = e.pageX || (e.clientX);
			dragY = e.pageY || (e.clientY);
			if (e.stopPropagation){ 
				// this code is for Mozilla and Opera 
				e.stopPropagation(); 
				e.preventDefault();
			}
		},
		mouseup:function(){
			dragX=-1;
		}
	});
}


//实时监控页面→机器人位置/机器人讲解视频/摄像头监控视频切换
var control_btnIndex = 1;//判断摄像头视频还是讲解视频：1/讲解视频;2/摄像头视频
var control_thumbIndex = 1;//机器人地图位置连接开启判断：1/开启中;2/已关闭 
var divChange = {
	videoChange: function () {
		$('.closeThumb').click(function () {//点击关闭机器人位置地图
			$(this).hide();
			$(this).siblings().show();
			control_thumbIndex = 2;
			$('.thumbPic').hide();
			if (control_btnIndex == 1) {
				$('.videoPic').show();
				$('.controlBtn1').hide();
				$('.controlBtn2').show();
			} else if (control_btnIndex == 2) {
				$('.expositoryPic').show();
			}
		});
		$('.openThumb').click(function () {//点击打开机器人位置地图
			$(this).hide();
			$(this).siblings().show();
			control_thumbIndex = 1;
			$('.thumbPic').show();
			$('.thumbPic').siblings().hide();
		});
		$('.videoBtn').click(function () {//点击摄像头监控视频按钮
			$(this).hide();
			$(this).siblings().show();
			control_btnIndex = 2;
			$('.videoPic').show();
			$('.videoPic').siblings().hide();
			$('.closeThumb').hide();
			$('.openThumb').show();
			control_thumbIndex = 2;
		});
		$('.expositoryBtn').click(function () {//点击机器人讲解视频按钮
			$(this).hide();
			$(this).siblings().show();
			control_btnIndex = 1;
			$('.expositoryPic').show();
			$('.expositoryPic').siblings().hide();
			$('.closeThumb').hide();
			$('.openThumb').show();
			control_thumbIndex = 2;
		})
	}
}
// 实时监控，左下角点击切换当前命令/接待人员信息
$('.control .txtMarquee-top .left').click(function(){
	$('.control .txtMarquee-top .bd-left').show();
	$('.control .txtMarquee-top .bd-right').hide();
})
$('.control .txtMarquee-top .right').click(function(){
	$('.control .txtMarquee-top .bd-right').show();
	$('.control .txtMarquee-top .bd-left').hide();
})
//实时监控，页面初始化加载
$('.leftNav li:nth-child(2)').click(function () {
	ajaxRobotOrderSubList();  //加载右下命令列表表格数据
	ajaxCurrentOrder();  //加载左下当前命令文本数据
	ajaxAppointPosition(); //加载指定区域列表
	picDrag('#plan-box', '#plan');	//图片拖动初始化
	initRobotVideo(); //播放机器人视频
});
// 实时监控，点击跳转其他页面清除定时
$('.leftNav li:not(:nth-child(2))').click(function () {
	if(window.robotPoisitionInterval){
		clearInterval(window.robotPoisitionInterval);
	}
});

// 实时监控，选择机器人
function controlPickRobot() {
	ajaxRobotPosition();
	initRobotVideo();
	if (window.robotPoisitionInterval) {
		clearInterval(window.robotPoisitionInterval);
	}
	window.robotPoisitionInterval = setInterval(ajaxRobotPosition, 5000);
}

//实时监控→开启前置摄像头按钮，点击事件
$('.control .top .btns:last-child .change button:first-child').click(function () {
	$(this).hide().siblings().show();
	ajaxRobotCamera(1);
});

//实时监控→关闭前置摄像头按钮，点击事件
$('.control .top .btns:last-child .change button:last-child').click(function () {
	$(this).hide().siblings().show();
	ajaxRobotCamera(0);
});

//实时监控→切换后台语音按钮，点击事件
$('.control .top .btns:last-child .voice button').click(function () {
	alert('切换后台语音');
	ajaxToggleVoice();
});

//实时监控→play按钮，点击事件
$('.control .direction .play').click(function () {
	$(this).hide();
	$('.control .direction .pause').show();
	ajaxRobotAction(0);
});

//实时监控→pause按钮，点击事件
$('.control .direction .pause').click(function () {
	$(this).hide();
	$('.control .direction .play').show();
	ajaxRobotAction(0);
});

//实时监控→前进按钮，点击事件
$('.control .direction .forward').click(function () {
	ajaxRobotAction(1);
});
//实时监控→后退按钮，点击事件
$('.control .direction .back').click(function () {
	ajaxRobotAction(2);
});
//实时监控→左转按钮，点击事件
$('.control .direction .left').click(function () {
	ajaxRobotAction(3);
});
//实时监控→右转按钮，点击事件
$('.control .direction .right').click(function () {
	ajaxRobotAction(4);
});

// 实时监控，选择指定区域
function controlAppointPosition(){
	ajaxTask();
}

// 实时监控，点击跳转任务按钮
$('.control .btns .turn-task').on('click', function () {
	ajaxTurnTask();
})

//实时监控，点击显示更多>>按钮，初始化加载表格数据及分页控件
$('.control .more').click(function () {
	$('.controlMoreModal').modal('show');
	initRobotOrderList({});
})

/*实时监控↑*/

/*权限管理↓*/

//权限管理界面，只有选择了人员，才显示该人员的具体权限
$('.permission select').on('hidden.bs.select', function (e) {

	var sel = $('.permission option:selected').val();

	if (sel != '' && sel != null) {
		$('.permissDiv').show();
		$('.permissName').html(sel + '当前管理权限');
	}
	//ajaxPost();

})

//权限管理界面，只有选择了人员，才显示该人员的具体权限
function selectOnchang(e) {

	console.log('只有选择了人员，才显示该人员的具体权限22222');
	var sel = $('.permission option:selected').val();

	// sel = parseInt(sel);

	if (admin_list.length < 1) return;

	var name;

	for (var i = 0; i < admin_list.length; i++) {

		var obj = admin_list[i];

		if (obj.id == sel) {

			name = obj.realName;

			break;
		}
	}


	if (sel != '' && sel != null) {
		$('.permissDiv').show();
		$('.permissName').html(name + ',当前管理权限');
	}

	//根据id获取管理员拥有的权限
	var p = {
		userId: sel
	}

	userPurview(p);

}


// $('.permissDiv td:nth-child(3)').click(function(){
//   alert('编辑这一行');
//   $('#permissEdit').show();
// });

//权限管理，新增(添加)机器人按钮事件
$('.permissBtn button.add').click(function () {
	//alert('新增一个机器人唷');
	$('.permissionModal').modal('show');
	console.log('新增');

	//点击新增 获取当前管理员未选择的机器人选项
	var userId = $('.permission option:selected').val();//管理人id
	// userId = parseInt(userId);

	var obj = {
		userId: userId,
		pageNum: 1,
		pageSize: 200
	}

	// 用户未选择的机器人列表
	purviewUnselectRobot(obj);


});

//权限管理，批量删除按钮事件
$('.permissBtn button.delete').click(function () {

	//获取勾选行的索引
	var checkedArr = new Array();

	$('.table-quanxian tbody tr td:nth-child(5) input:checked').each(function (i) {

		var index = $(this).parents('tr').index();
		checkedArr[i] = index;
	});

	console.log('checkedArr', checkedArr);

	if (checkedArr.length < 1) {
		alert('请选择需要保存的预约');
		return;
	}

	var ary_save = [];

	for (var i = 0; i < checkedArr.length; i++) {

		var a = checkedArr[i];
		var obj = quanxian_list[a];
		ary_save.push(obj);
	}

	deletePurview(ary_save);

});
//权限管理,添加权限
$('.role-btn').click(function () {

	var userId = $('.permission option:selected').val();//管理人id
	// userId = parseInt(userId);

	var robotId = $('.permissionModal .modal-body .form-row .pickRobot option:selected').val();//机器人ID
	robotId = parseInt(robotId);

	var ary = [];
	$(".permissionModal input[type=checkbox]:checked").each(function () {

		console.log('权限管理确定按钮', $(this).attr('value'));

		var checkedItem = parseInt($(this).attr('value'));
		ary.push(checkedItem);
	})

	var obj = {
		userId: userId,
		robotId: robotId,
		roleIds: ary
	}

	addRole(obj);

})

// $('.appointment .editTable').on('click','td:nth-child(7)',function()
//权限管理，编辑按钮事件
$('.permissDiv .table-quanxian tbody').on('click', 'td:nth-child(3)', function () {

	$('#permissEdit').show();

});

//权限管理，编辑之后打勾按钮事件
$('.submit-bianji').click(function () {
	console.log('编辑之后打勾');

	var userId = $('.permission option:selected').val();//管理人id
	// userId = parseInt(userId);

	var robotId = $('#permissEdit .permissEditRobot .pickRobot option:selected').val();//机器人ID
	robotId = parseInt(robotId);

	var ary = [];
	$(".checkbox-bianji input[type=checkbox]:checked").each(function () {

		console.log('编辑权限管理', $(this).attr('value'));

		var checkedItem = parseInt($(this).attr('value'));
		ary.push(checkedItem);
	})

	console.log(userId, robotId, ary);

	if (userId == NaN || robotId == NaN) {
		alert('检查是否有未选择的选项');
		return;
	}

	var obj = {
		userId: userId,
		robotId: robotId,
		roleIds: ary
	}
	updatePurview(obj)


	$('#permissEdit').hide();

})
//权限管理，单个删除按钮事件
$('.permissDiv .table-quanxian tbody').on('click', 'td:nth-child(4)', function () {

	var deleteRow = $(this).parent().index();

	alert('删除的数据:' + deleteRow);

	//将被删除的数据
	var obj = quanxian_list[deleteRow];

	console.log('shan', obj);
	var ary = [];
	var p = {
		userId: obj.userId,
		robotId: obj.robotId
	}
	ary.push(p);

	deletePurview(ary);

});

//权限管理，编辑区保存按钮点击事件
$('.permissEdit button').click(function () {
	alert('保存好了');
	$('.permissEdit').hide();
});

/*权限管理↑*/

/* 配置管理界面↓*/

//配置管理→系统配置→机器人负责人配置，初始化加载
$('.leftNav li:nth-child(4)').click(function (e) {
	e.preventDefault();


	var robotId = 1;

	var obj = {
		robotId: robotId
	}

	initPersonCharge(obj);



});
//配置管理→系统配置→机器人负责人配置，选择机器人
function pz_selectOnchang(e) {

	var robotId = $('.settings option:selected').val();//获取机器人的id
	robotId = parseInt(robotId);

	console.log('选择机器人。11', robotId);

	var obj = {
		robotId: robotId
	}

	initPersonCharge(obj);
}

//配置管理→系统配置→机器人负责人配置→添加负责人按钮点击事件
$('.personCharge > .sel > button').click(function () {
	$('.personCharge > .sel > div').show();
});

//配置管理→系统配置→机器人负责人配置→确认添加按钮点击事件
$('.personCharge > .sel .confirm').click(function () {
	$('.personCharge > .sel > div').hide();

	//获取姓名 电话 邮件
	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		robotId = 1;
		// alert('请选择机器人');
		// return;
	}
	robotId = parseInt(robotId);

	var personName = $('.personCharge #pz_name').val();//姓名
	var personPhone = $('.personCharge #pz_phone').val();//电话
	var personEmail = $('.personCharge #pz_email').val();//邮件
	var type = 0;

	console.log(robotId, personName, personPhone, personEmail);
	if (personName == null || personPhone == null || personEmail == null) {
		alert('请填写完整');
		return;
	}

	var obj = {
		robotId: robotId,
		personName: personName,
		personPhone: personPhone,
		personEmail: personEmail,
		type: type
	}

	//负责人添加
	add_responsibleperson(obj);

});

//配置管理→系统配置→机器人负责人配置→表格编辑按钮点击事件
$('.tablePersonCharge tbody').on('click', 'td:nth-child(6)', function () {
	var has = $(this).parent().hasClass('mainPerson');
	if (has) {
		alert('主要负责人不能编辑');
	} else {
		$(this).parent().find('td:nth-child(2)').editable();
		$(this).parent().find('td:nth-child(3)').editable();
		$(this).parent().find('td:nth-child(4)').editable();
	}

	var deleteRow = $(this).parent().index();

	alert('编辑的数据:' + deleteRow);


});

//配置管理→系统配置→机器人负责人配置→表格删除按钮点击事件
$('.tablePersonCharge tbody').on('click', 'td:nth-child(7)', function () {

	var has = $(this).parent().hasClass('mainPerson');
	if (has) {
		alert('主要负责人不能删除');
	} else {
		alert('删除此辅助负责人');
	}

	var robotId = $('.settings option:selected').val();//获取机器人的id
	robotId = parseInt(robotId);

	var deleteRow = $(this).parent().index();

	alert('删除的数据:' + deleteRow);
	var ids = [];

	var obj = responsibleperson_list[deleteRow];

	ids.push(obj.id);

	var p = {
		robotId:robotId,
		ids:ids
	}

	deleteResponsibleperson(p);


});

//配置管理→系统配置→机器人负责人配置→删除按钮事件
$('#deleteMore').click(function () {

	alert('删除所选辅助负责人9999');

	var robotId = $('.settings option:selected').val();//获取机器人的id
	robotId = parseInt(robotId);
	//获取勾选行的索引
	var checkedArr = new Array();

	$('.tablePersonCharge_set tbody tr td:nth-child(8) input:checked').each(function (i) {

		var index = $(this).parents('tr').index();

		checkedArr[i] = index;

		alert(i);
	});

	console.log('checkedArr', checkedArr);

	if (checkedArr.length < 1) {
		alert('请选择需要删除的负责人配置');
		return;
	}
	var ary_save = [];
	for (var i = 0; i < checkedArr.length; i++) {
		var a = checkedArr[i];

		var obj = responsibleperson_list[a];

		ary_save.push(obj.id);
	}
	
	var p = {
		robotId:robotId,
		ids:ary_save
	}
	deleteResponsibleperson(p);

});

//配置管理→系统配置→机器人负责人配置→保存按钮事件
$('#saveMore').click(function () {

	//获取勾选行的索引
	var checkedArr = new Array();

	$('.settings .tab-content .tableDiv tbody tr td:nth-child(8) input:checked').each(function (i) {

		var index = $(this).parents('tr').index();
		checkedArr[i] = index;
	});

	console.log('checkedArr', checkedArr);

	if (checkedArr.length < 1) {
		alert('请选择需要保存的负责人配置');
		return;
	}

	var ary_save = [];
	// responsibleperson_list
	for (var i = 0; i < checkedArr.length; i++) {
		var a = checkedArr[i];
		var personName = '.tablePersonCharge_set tbody tr:eq(' + a + ') td:eq(1)';
		personName = $(personName).text();

		var personPhone = '.tablePersonCharge_set tbody tr:eq(' + a + ') td:eq(2)';
		personPhone = $(personPhone).text();

		var personEmail = '.tablePersonCharge_set tbody tr:eq(' + a + ') td:eq(3)';
		personEmail = $(personEmail).text();

		console.log(personName, personPhone, personEmail);

		var obj = responsibleperson_list[i];
		var p = {
			personName: personName,
			personPhone: personPhone,
			personEmail: personEmail,
			robotId: obj.robotId,
			id: obj.id,
			type: obj.type
		}
		ary_save.push(p)

	}
	console.log('ary_save', ary_save, 'responsibleperson_list', responsibleperson_list);

	updataResponsibleperson(ary_save);


	// console.log('你好',$('.tablePersonCharge_set tbody tr:eq(0) td:eq(1)').text());

});
//切换到配置管理→系统配置→机器人形象库，初始化加载
$('.systemConfiguration li:nth-child(2)').click(function (e) {

	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		robotId = 1;
		// alert('请选择机器人');
		// return;
	}
	e.preventDefault();
	robotId = parseInt(robotId);
	var obj = {
		robotId: robotId,
		pageNum: 1,
		pageSize: 200
	}


	ajaxImage(obj);
});

//配置管理→系统配置→机器人形象库→点击选择图片
$('.imageLibrary .picList').on('click', 'li', function () {
	$(this).siblings().removeClass('checked');
	$(this).addClass('checked');

	selectRow = $(this).parent().index();

	// alert('选择的图片:' + selectRow);
});
//配置管理→系统配置→机器人形象库→点击选择
$('#robotImageSelect').on('click', function () {
	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		robotId = 1;
		// alert('请选择机器人');
		// return;
	}
	robotId = parseInt(robotId);

	var obj = robotImage_list[selectRow];
	obj.robotId = robotId;

	console.log('obj', obj);

	enableRobotImage(obj);
})

//配置管理→系统配置→机器人形象库→点击加号添加图片
$('.imageLibrary .picList').on('click', 'li.add', function () {
	// alert('添加形象');

});


//配置管理→系统配置→机器人形象库→点击加号添加/上传图片
function getPhoto(e) {
	var file = null;
	if (e.files && e.files[0]) {

		file = e.files[0];

	} else if (e.files && e.files.item(0)) {

		file = e.files.item(0);

	}
	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		// alert('请选择机器人');
		// return;
		robotId = 1;
	}
	robotId = parseInt(robotId);

	if (file.type.startsWith('image')) {

		var data=new FormData();
		data.append('content.imageName','机器人形象图');
		data.append('imageFile',file);

		var p = {
			fileData:data,
			robotId:robotId
		}

		addRobotImage(p);


	} else {
		alert("请选择图片！");

		return;
	}

}


//切换到配置管理→系统配置→场馆地图库，初始化加载
$('.systemConfiguration li:nth-child(3)').click(function (e) {
	e.preventDefault();
	//
	var exhibitionhallId = 1;//默认先用二层的展厅id
	var obj = {
		pageNum: 1,
		pageSize: 100,
		exhibitionhallId: 1
	}
	ajaxVenue(obj);
});

//切换到配置管理→系统配置→场馆地图库--》点击选择图片
$('.venueMap .picList').on('click', 'li', function () {
	$(this).siblings().removeClass('checked');
	$(this).addClass('checked');

	selectMapRow = $(this).parent().index();

	// alert('选择的图片:' + selectRow);
});

//切换到配置管理→系统配置→场馆地图库--选择，初始化加载
$('#mapSelect').on('click', function () {

	alert('选择场馆地图');
	var obj = exhibitionhall_mapList[selectMapRow];
	var p = {
		exhibitionhallId: 1,
		mapLibId: obj.id
	}
	enableMapImage(p);


});
//切换到配置管理→系统配置→场馆地图库--添加，初始化加载
function getMapPhoto(e) {
	var file = null;
	if (e.files && e.files[0]) {

		file = e.files[0];

	} else if (e.files && e.files.item(0)) {

		file = e.files.item(0);

	}

	if (file.type.startsWith('image')) {

        var data=new FormData();
		data.append('content.mapName','展厅地图');
		data.append('mapFile',file);
		
		addMap(data);


	} else {
		alert("请选择图片！");

		return;
	}

};

//切换到配置管理→系统配置→机器人最低电量,初始化加载
function changeEnergy(e) {
	var energyVal = $('#electricLow .pickEnergy option:selected').val();//机器人ID
	energyVal = parseInt(energyVal);


	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		// alert('请选择机器人');
		// return;
		robotId = 1;
	}
	robotId = parseInt(robotId);
	console.log('energyVal', energyVal, 'robotId', robotId);

	var obj = {
		robotId: robotId,
		electricAmount: energyVal
	}

	setMinEnergy(obj);
}

//切换到配置管理→业务配置→板块及展板配置，初始化加载
$('.settings > .tab > ul > li:nth-child(2)').click(function (e) {
	e.preventDefault();
	var obj = {
		exhibitionhallId: 1,
		pageNum: 1,
		pageSize: 100
	}
	ajaxPlate(obj);

	ajaxPanel(obj);
});

//配置管理→业务配置→板块及展板配置→板块配置、展板配置切换
$('.platePanel #platePz-btn').click(function () {
	$(this).addClass('active');
	$(this).siblings().removeClass('active');
	$('.platePanel .tableDiv table:first-child').show();
	$('.platePanel .tableDiv table:last-child').hide();

	// $('.platePanel #panelDelect-btn').addClass('hideit');
	$('#panelDelect_btn_parent').addClass('hideit');
});

$('.platePanel #panelPz-btn').click(function () {
	$(this).addClass('active');
	$(this).siblings().removeClass('active');
	$('.platePanel .tableDiv table:last-child').show();
	$('.platePanel .tableDiv table:first-child').hide();

	// $('#panelDelect-btn').show();
	// $('.platePanel #panelDelect-btn').removeClass('hideit');
	$('#panelDelect_btn_parent').removeClass('hideit');
});


//配置管理→业务配置→板块及展板配置→板块配置→点击编辑按钮，颜色变成可编辑
$('.platePanel table:first-child tbody').on('click', 'td.edit', function () {
	$(this).siblings().find('input').removeAttr("disabled");

	editRow = $(this).parent().index();

	// alert('编辑的数据:' + editRow);

});
//配置管理→业务配置→板块及展板配置→板块配置→点击编辑按钮，提交编辑内容
function chkvalue(e) {

	var color = '.tablePlate tbody tr:eq(' + editRow + ') td:eq(2) input';
	color = $(color).val();

	var obj = plate_list[editRow];

	console.log('color', color, editRow, obj);

	var p = {
		productId: obj.productId,
		productName: obj.productName,
		productRemark: obj.productRemark,
		exhibitionhallId: obj.exhibitionhallId,
		backgroundColor: color
	}

	updataProduct(p);


};
//配置管理→业务配置→板块及展板配置→板块配置→点击删除按钮
$('.platePanel table:first-child tbody').on('click', 'td.delete', function () {

	var deleteRow = $(this).parent().index();
	// alert('删除的数据:' + deleteRow);

	alert('删除这个板块');
	var obj = plate_list[deleteRow];

	deleteExhibitionhallProduct(obj);
});

//配置管理→业务配置→板块及展板配置→展板配置→点击删除按钮（删除单个）
$('.tablePanel tbody').on('click', 'td.delete', function () {

	var deleteRow = $(this).parent().index();
	alert('删除的数据:' + deleteRow);

	alert('删除这个展板');
	var obj = plate_list[deleteRow];

	var ary_save = [];

	var obj = pannel_list[deleteRow];
	ary_save.push(obj.plateId);

	deletePlate(ary_save);

});

//配置管理→业务配置→板块及展板配置→展板配置→点击编辑按钮，变成可编辑
$('.tablePanel tbody').on('click', 'td.edit', function () {
	$(this).siblings().find('input').removeAttr("disabled");

	editRow_pannel = $(this).parent().index();

	// alert('编辑的数据:' + editRow_pannel);

});
//配置管理→业务配置→板块及展板配置→保存按钮
$('.platePanel #panelSave-btn').click(function () {
	$(this).addClass('active');
	$(this).siblings().removeClass('active');

	// alert('保存提交'); 

	//获取checkbox， 遍历数组  从数组中获取对应的item

	//获取勾选行的索引
	var checkedArr = new Array();

	$('.tablePanel tbody tr td:nth-child(8) input:checked').each(function (i) {

		var index = $(this).parents('tr').index();
		checkedArr[i] = index;

		console.log('checkedArr');
	});

	console.log('checkedArr', checkedArr);

	if (checkedArr.length < 1) {
		alert('请选择需要保存的数据');
		return;
	}

	var ary_save = [];
	for (var i = 0; i < checkedArr.length; i++) {

		var a = checkedArr[i];
		var obj = pannel_list[a];

		var productName = '.tablePanel tbody tr:eq(' + a + ') td:eq(1) input';
		productName = $(productName).val();

		var plateRemark = '.tablePanel tbody tr:eq(' + a + ') td:eq(2) input';
		plateRemark = $(plateRemark).val();

		var plateName = '.tablePanel tbody tr:eq(' + a + ') td:eq(3) input';
		plateName = $(plateName).val();

		var plateWeight = '.tablePanel tbody tr:eq(' + a + ') td:eq(4) input';
		plateWeight = $(plateWeight).val();

		var p = {
			productName: productName,
			plateRemark: plateRemark,
			plateName: plateName,
			plateWeight: plateWeight,
			plateId: obj.plateId
		}
		ary_save.push(p)
	}

	console.log('ary_save', ary_save);
	//更新保存 并刷新
	updatePlate(ary_save);


})
//配置管理→业务配置→板块及展板配置→删除按钮(删除多个)
$('.platePanel #panelDelect-btn').click(function () {
	$(this).addClass('active');
	$(this).siblings().removeClass('active');

	alert('删除提交');

	//获取勾选行的索引
	var checkedArr = new Array();

	$('.tablePanel tbody tr td:nth-child(8) input:checked').each(function (i) {

		var index = $(this).parents('tr').index();
		checkedArr[i] = index;

		console.log('checkedArr');
	});

	console.log('checkedArr', checkedArr);

	if (checkedArr.length < 1) {
		alert('请选择需要删除的数据');
		return;
	}

	var ary_save = [];
	for (var i = 0; i < checkedArr.length; i++) {

		var a = checkedArr[i];
		var obj = pannel_list[a];
		ary_save.push(obj.plateId);
	}
	deletePlate(ary_save);

});


//切换到配置管理→业务配置→机器人名称配置，初始化加载
$('.workTab li:nth-child(2)').click(function (e) {
	e.preventDefault();

	var obj = {
		exhibitionhallId: 1,//目前选择二层展厅
	}

	ajaxRobot(obj);
});

//配置管理→业务配置→机器人名称配置→点击新增机器人按钮，弹窗
$('.robotName button').click(function () {
	$('.robotNameModal').modal('show');
});

//新增机器人弹窗'确定'按钮事件
$('.modal-footer button.confirm').click(function () {
	$('.modal').modal('hide');
	$('.robotSucceedModal').modal('show');

	console.log('确定');

	var robotName = $('.robotNameModal #robotName_add').val();
	var primaryResponsiblePersonName = $('.robotNameModal #robotPerson_add').val();
	var startUserDate = $('.robotNameModal #robotData_add').val();//startUserDate
	var maintainTime = $('.robotNameModal #robotWei_add').val();

	// console.log(robotName, primaryResponsiblePersonName, startTate, maintainTime);
	var obj = {
		exhibitionhallId: 1,//目前默认二层展厅
		robotName: robotName,
		primaryResponsiblePersonName: primaryResponsiblePersonName,
		startUserDate: startUserDate,
		maintainTime: maintainTime
	}

	addRobot(obj);

});

//机器人添加成功弹窗，点击关闭按钮，刷新机器人名称配置表格
$('.robotSucceedModal .modal-header button').click(function () {
	$('.robotSucceedModal').modal('hide');
	console.log('关闭');
	var obj = {
		exhibitionhallId: 1,//目前选择二层展厅
	}
	ajaxRobot(obj);
});

//切换到配置管理→业务配置→讲解词、语言库配置，动态加载
$('.settings > .tab > ul > li:nth-child(2)').click(function (e) {
	e.preventDefault();

	var obj = {
		exhibitionhallId: 1,//目前默认二层展厅
		pageNum: 1,
		pageSize: 10
	}
	initCommentary(obj);
});

//配置管理→业务配置→讲解词、语言库配置/表情库配置，button模拟input上传文件
$('.tableDiv').on('click', 'button.upload', function () {

	var upLoadRow = $(this).parent().parent().index();
	alert(upLoadRow);
	
	var p = jiangjie_list[upLoadRow];

	$(this).parent().next().find('input').trigger('click');

	

	//https://blog.csdn.net/qq_22560431/article/details/80883223 js上传文件(压缩文件)
	// https://blog.csdn.net/m0_37054738/article/details/78706885

	var fileObj = document.getElementById("fileUPLoad").files[0]; // js 获取文件对象  

	  
	var data = new FormData(); // FormData 对象

	data.append('scriptFile',fileObj);
	data.append('content.scriptFileName',fileObj.name);
	data.append('content.plateId',p.plateId);

    addJiangjie(data);

});

//配置管理→业务配置→讲解词、语言库配置/表情库配置，选择文件后，前一个td获取文件名
$('.commentary .tableDiv').on('change', 'input', function () {
	//上传文件，也可以在这个时候进行。上传完成后，再把文件名显示在脚本td
	var file = this.files[0];
	$(this).parent().prev().prev().html(file.name);
});

//切换到配置管理→业务配置→表情库配置，初始化加载
$('.workTab li:nth-child(4)').click(function (e) {

	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		// alert('请选择机器人');
		// return;
		robotId = 1;
	}
	robotId = parseInt(robotId);

	var obj = {
	  pageNum:1,
	  pageSize:10,
	  robotId:robotId
	}
	initEmoji(obj);

	e.preventDefault();
});

//配置管理→业务配置→表情库配置，button模拟input上传文件
$('.tableDiv').on('click', 'button.uploadEmoji', function () {

	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		// alert('请选择机器人');
		// return;
		robotId = 1;
	}
	robotId = parseInt(robotId);


	var upLoadRow = $(this).parent().parent().index();
	
	var p = emoji_list[upLoadRow];
	console.log('表情库0000',upLoadRow,p);

	$(this).parent().next().find('input').trigger('click');


	var fileObj = document.getElementById("emojiUPLoad").files[0]; // js 获取文件对象  

	  
	var data = new FormData(); // FormData 对象

	data.append('expressionFile',fileObj);
	data.append('content.expressionName',p.expressionName);
	data.append('content.id',p.id);
	data.append('content.expressionFileName',fileObj.name);
	console.log(data,'表情库');

	var obj = {
		robotId:robotId,
		fileData:data
	}
	
    UpdataExpression(obj);

});

//切换到配置管理→业务配置→表情库配置--启用，初始化加载
$('body').on('click','.tableEmoji #in-use',function(){ 	
	
	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		// alert('请选择机器人');
		// return;
		robotId = 1;
	}
	robotId = parseInt(robotId);

	var row = $(this).parents('tr').index();
	console.log(row,'表情库启用');
	var obj = emoji_list[row];

	var status = null;
	
	if(obj.isEnable==1){
		status = 0;
	}else{
		status = 1;
	}
    var p = {
	  robotId:robotId,
	  isEnable:status,
	  expressionLibId:obj.id
	}
	console.log('p',p);
	enableExpression(p);

});

//配置管理→广告配置→机器人巡逻路线，途径点位的点击选择div
$('.tablePatrolRoute tbody').on('click', 'td:last-child > div:first-child', function () {
	
	var showRow = $(this).parent().parent().index();

	var obj = point_list[showRow];
	// alert(showRow);

	// alert('弹出');
	$('.mapModal').modal('show');

	//计算图片的高度,图片的宽度：1000-50-50 = 900
	 var img_width = 900*1687/3000;

	 var bili = 900/3000;

	 //在地图上的坐标
	 var x = obj.x * bili;
	 var y = obj.y * bili;
	 
	 $(".mapModal .modal-body .form-row").find("img[id='robot_img']").remove();

	 $('#points_robot').html('机器人位置坐标 ：x:'+obj.x+',y:'+obj.y+'');

	 $(".mapModal .modal-body .form-row").append('<img src="img/u88.png" alt="机器人" id="robot_img" style="position: absolute;left:'+x+'px;top:'+y+'px;">');
});

//配置管理→广告配置→广告播放列表，初始化加载
$('.settings > .tab > ul > li:nth-child(3)').click(function (e) {
	e.preventDefault();

	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		robotId = 1;
		// alert('请选择机器人');
		// return;
	}
	robotId = parseInt(robotId);

	var obj = {
	  robotId:robotId,
	  pageNum:1,
	  pageSize:10
	}
	initADList(obj);
});

//配置管理→广告配置→广告播放列表→点击删除按钮
$('.tableADList tbody').on('click', 'td.delete', function () {

    var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		robotId = 1;
		// alert('请选择机器人');
		// return;
	}
	robotId = parseInt(robotId);

	var deleteRow = $(this).parent().index();
	alert('广告删除的数据:' + deleteRow);
	var obj = ad_list[deleteRow];


	var ary = [];
	ary.push(obj.id);

    var obj = {
		ids:ary,
		robotId:robotId
	}
    deleteAdvertisement(obj);

});
//配置管理→广告配置→广告播放列表→启用
$('body').on('click','.tableADList #ad_inuse',function(){ 

	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		robotId = 1;
		// alert('请选择机器人');
		// return;
	}
	robotId = parseInt(robotId);

	var row = $(this).parents('tr').index();
	alert(row);

	var obj = ad_list[row];

	var status = null;
	
	if(obj.isEnable==1){
		status = 0;
	}else{
		status = 1;
	}
	console.log('obj',obj);
    var p = {
	  isEnable:status,
	  adId:obj.adId,
	  robotId:robotId
	}
	console.log('p',p);
	enableAdvertisement(p);
})
//配置管理→广告配置→广告播放列表→往上移
$('body').on('click','.tableADList tbody tr td.up',function(){
	var row = $(this).parents('tr').index();

	console.log('往上移',row);
   var obj =  ad_list[row];

   var p = {
	adId:obj.adId,
	sequence:'往上移'
   }

   paixun(p);
	
})
//配置管理→广告配置→广告播放列表→往下移
$('body').on('click','.tableADList tbody tr td.down',function(){
	var row = $(this).parents('tr').index();
	console.log('往下移');

	var obj =  ad_list[row];

	var p = {
	 adId:obj.adId,
	 sequence:'往下移'
	}

	paixun(p);
})

//配置管理→广告配置→广告播放列表→置顶
$('body').on('click','.tableADList tbody tr td.top',function(){
	var row = $(this).parents('tr').index();
	console.log('置顶');

	var obj =  ad_list[row];

	var p = {
	 adId:obj.adId,
	 sequence:'置顶'
	}

	paixun(p);
})

//配置管理→广告配置→广告播放列表→置底
$('body').on('click','.tableADList tbody tr td.bottom',function(){
	var row = $(this).parents('tr').index();
	console.log('置底');

	var obj =  ad_list[row];

	var p = {
	 adId:obj.adId,
	 sequence:'置底'
	}

	paixun(p);
})
//配置管理→广告配置→广告播放列表,广告排序
function paixun(parameter){

	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		robotId = 1;
		// alert('请选择机器人');
		// return;
	}
	robotId = parseInt(robotId);
	
	
	var p = {
	  robotId:robotId,
	  adId:parameter.adId,
	  sequence:parameter.sequence
	}
   
	sortAdvertisement(p);

}
//配置管理→广告配置→广告播放列表→多选删除
$('.adPlaylist #deleteAD_btn').click(function () {
	$(this).addClass('active');

	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		robotId = 1;
		// alert('请选择机器人');
		// return;
	}
	robotId = parseInt(robotId);
	
	alert('删除提交-广告');

	// 获取勾选行的索引
	var checkedArr = new Array();

	$('.tableADList tbody tr td:nth-child(10) input:checked').each(function (i) {

		var index = $(this).parents('tr').index();
		checkedArr[i] = index;

	});

	console.log('checkedArr', checkedArr);

	if (checkedArr.length < 1) {
		alert('请选择需要删除的数据');
		return;
	}

	var ary_save = [];
	for (var i = 0; i < checkedArr.length; i++) {

		var a = checkedArr[i];
		var obj = ad_list[a];
		ary_save.push(obj.id);
	}
	var p = {
		ids:ary_save,
		robotId:robotId
	}
    deleteAdvertisement(p);

});


//配置管理→广告配置→新增广告，初始化加载
$('#add_newAD').click(function(e){
	// alert('新增广告');
	$('.ADModal').modal('show');
})
//配置管理→广告配置→上传文件，初始化加载
$('.upload_ad').click(function(){

	$(this).find('input').trigger('click');
})

function getADFile(e){
	AD_file = null;
	var file = null;
	if (e.files && e.files[0]) {

		file = e.files[0];

	} else if (e.files && e.files.item(0)) {

		file = e.files.item(0);
	}

	AD_file = {
	  adName:file.name,
	  adFile:file
	}

	$('.ADModal #ad_btn input').parent().find('span').text(file.name);
}
//配置管理→业务配置→讲解词、语言库配置/表情库配置，选择文件后，前一个td获取文件名
// $('.ADModal #ad_btn').on('change', 'input', function () {
// 	//上传文件，也可以在这个时候进行。上传完成后，再把文件名显示在脚本td
// 	// var file = this.files[0];
	
	
// });

//配置管理→广告配置→搜索广告，
function searchADS(e){
	
	var str = $('#seach_ad').val();
	console.log('搜索',str);

	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		robotId = 1;
		// alert('请选择机器人');
		// return;
	}
	robotId = parseInt(robotId);

	var obj = {
		adName:str,
		robotId:robotId
	}
	
    searchAdvertisement(obj);
}
//配置管理→广告配置→确定新增广告
$('.confirm-ad').click(function(e){
	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		robotId = 1;
		// alert('请选择机器人');
		// return;
	}
	robotId = parseInt(robotId);
	
	var adName = $('.ADModal #ad-name').val();
	var obj = {
		adName:adName,
		adFile:AD_file.adFile,
		robotId:robotId
	}
	
	addAdvertisement(obj);

	$('.ADModal').modal('hide');
	
})

//配置管理→广告配置→广告触发规则，初始化加载
$('.adTab li:nth-child(2)').click(function (e) {
	e.preventDefault();

	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		robotId = 1;
		// alert('请选择机器人');
		// return;
	}
	robotId = parseInt(robotId);
	
	var obj = {
	  pageNum:1,
	  pageSize:10,
	  robotId:robotId
	}
 
	initADRules(obj);
});
//配置管理→广告配置→广告触发规则--启用，初始化加载
$('body').on('click','.tableADRules #rule_inuse',function(){ 

	var robotId = $('.settings option:selected').val();//获取机器人的id
	if (robotId == null || robotId == '' || robotId == undefined) {
		robotId = 1;
		// alert('请选择机器人');
		// return;
	}
	robotId = parseInt(robotId);

	var row = $(this).parents('tr').index();
	alert(row);

	var obj = adRules_list[row];

	var status = null;
	
	if(obj.isEnable==1){
		status = 0;
	}else{
		status = 1;
	}
	console.log('obj',obj);
    var p = {
	  robotId:robotId,
	  adRuleId:obj.adRuleId,
	  playTimeDuration:obj.playTimeDuration,
	  isEnable:status
	}
	console.log('p',p);
	enableTriggerrule(p);
})

//配置管理→广告配置→机器人巡逻路线，初始化加载
$('.adTab li:nth-child(3)').click(function (e) {
	e.preventDefault();
	var obj = {
		exhibitionhallId:1//目前默认选择二层展厅
	}
	initPatrolRoute(obj);
});

//配置管理→日志、维修、告警配置→日志服务器配置，初始化加载
$('.settings > .tab > ul > li:nth-child(4)').click(function (e) {
	e.preventDefault();

	var obj = {}
	initServers(obj);
	
});
//配置管理→日志、维修、告警配置→日志服务器配置，启用
$('body').on('click','.tableServers .serverInUse',function(){  

	var row = $(this).parents('tr').index();
	alert(row);

	var obj = log_list[row];

	var status = null;
	
	if(obj.isEnable==1){
		status = 0;
	}else{
		status = 1;
	}
    var p = {
	  isEnable:status,
	  id:obj.id
	}
	console.log('p',p);
	enableRobotLog(p);

});

//配置管理→日志、维修、告警配置→告警信息级别配置，初始化加载
$('.otherTab li:nth-child(2)').click(function (e) {
	e.preventDefault();
	var obj = {}
	initAlarmLevel(obj);
});
//配置管理→日志、维修、告警配置→告警信息级别配置--新增，
$('body').on('click','#addEvent',function(){
	
	$('.msgModal').modal('show');
});
//配置管理→日志、维修、告警配置→告警信息级别配置--新增 -- 确定，
$('.confirm-msg').click(function(e){
	
	
	var eventName = $('.msgModal #msg-name').val();
	var level = $('.msgModal #msg_level').val();
	var reactionTime = $('.msgModal #msg_time').val();

	var obj = {
		eventName:eventName,
		level:level,
		reactionTime:reactionTime
	}
	
	console.log('obj',obj);

	addLevel(obj);

	$('.msgModal').modal('hide');
	
})
//配置管理→日志、维修、告警配置→告警信息级别配置--删除多个告警信息，初始化加载
$('body').on('click','#deleteMoreEvent',function(){
	alert('删除多个告警信息');
	
	//获取勾选行的索引
	var checkedArr = new Array();

	$('.tableAlarmLevel tbody tr td:nth-child(7) input:checked').each(function (i) {

		var index = $(this).parents('tr').index();
		checkedArr[i] = index;
	});

	console.log('checkedArr', checkedArr);

	if (checkedArr.length < 1) {
		alert('请选择需要删除的数据');
		return;
	}

	var ary_save = [];
	for (var i = 0; i < checkedArr.length; i++) {

		var a = checkedArr[i];
		var obj = level_list[a];
		ary_save.push(obj.id);
	}
	deleteLevel(ary_save);


});
//配置管理→日志、维修、告警配置→告警信息级别配置→点击删除按钮
$('.tableAlarmLevel tbody').on('click', 'td.delete', function () {

	var deleteRow = $(this).parent().index();
	// alert('告警配置删除的数据:' + deleteRow);
	var obj = level_list[deleteRow];

	alert(deleteRow);
	var ary = [];
	ary.push(obj.id)

    deleteLevel(ary);

});
//配置管理→日志、维修、告警配置→告警信息级别配置--保存多个告警信息，初始化加载
$('body').on('click','#saveMoreEvent',function(){
//获取勾选行的索引
	var checkedArr = new Array();

	$('.tableAlarmLevel tbody tr td:nth-child(7) input:checked').each(function (i) {

		var index = $(this).parents('tr').index();
		checkedArr[i] = index;
	});

	console.log('checkedArr', checkedArr);

	if (checkedArr.length < 1) {
		alert('请选择需要保存的数据');
		return;
	}

	var ary_save = [];

	for (var i = 0; i < checkedArr.length; i++) {
        
		var a = checkedArr[i];
		var obj = level_list[a];
		// ary_save.push(obj.id);

	   var a = $('.tableAlarmLevel tbody tr td:nth-child(3)').text();
	   var b = $('.tableAlarmLevel tbody tr td:nth-child(4)').text();
	   var c = $('.tableAlarmLevel tbody tr td:nth-child(2)').text();
	   var p = {
		id: obj.id,
		eventName:c,
		level:a,
		reactionTime:b
	   };

	   ary_save.push(p)
	
	}
   console.log('ary_save',ary_save);
   
    updataLevel(ary_save);

    

})
//配置管理→日志、维修、告警配置→告警信息级别配置→点击编辑按钮
$('.tableAlarmLevel tbody').on('click','td.edit',function(){
	
	$(this).parent().find('td:nth-child(2)').editable();
	$(this).parent().find('td:nth-child(3)').editable();
	$(this).parent().find('td:nth-child(4)').editable();
	

	var deleteRow = $(this).parent().index();

	alert('告警配置编辑的数据:' + deleteRow);
})
/*配置管理↑*/

/*监测预警↓*/

//监测预警，表格，初始化加载
$('.leftNav li:nth-child(3)').click(function (e) {
	e.preventDefault();
	var data = {};
	initMonitor(data);
});

//监测预警，点击“筛选”按钮，刷新列表
$('.monitor .button > button').on('click', function (e) {
	var robotId = $('.monitor .pickRobot option:selected').val();//机器人ID
	robotId = parseInt(robotId);

	var responsiblePersonId = $('.monitor .pickNotifier option:selected').val();//预约人ID
	// responsiblePersonId = parseInt(responsiblePersonId);

	var startTime = document.getElementById('monitorStartTime').value;
	var endTime = document.getElementById('monitorEndTime').value;
	
	var sortType = $('.monitor .pickSort option:selected').val();
	// sortType = parseInt(sortType);

	var data = {
		robotId: robotId,
		responsiblePersonId: responsiblePersonId,
		startTime: startTime,
		endTime: endTime,
		sortType: sortType
	}

	console.log('监测预警',data);

	initMonitor(data);
});

//监测预警，排序select选择后，刷新列表
$('.monitor .pickSort').on('hide.bs.dropdown', function (e) {

	var robotId = $('.monitor .pickRobot option:selected').val();//机器人ID
	var responsiblePersonId = $('.monitor .pickNotifier option:selected').val();//预约人ID
	var startTime = document.getElementById('monitorStartTime').value;
	var endTime = document.getElementById('monitorEndTime').value;
	var sortType = $('.monitor .pickSort option:selected').val();

	var data = {
		robotId: robotId,
		responsiblePersonId: responsiblePersonId,
		startTime: startTime,
		endTime: endTime,
		sortType: sortType
	}

	console.log(data)

	initMonitor(data);
});

/*监测预警↑*/

/*预约管理↓*/

//预约管理，展示表格，初始化加载
$('.leftNav li:nth-child(6)').click(function (e) {
	e.preventDefault();

	var data = {};
	initAppointmentShow(data);
});

//预约管理，展示表格界面，点击“查询”按钮，刷新列表
$('.appointment .showTable .button > button').on('click', function (e) {
	//	ajaxPost();

	var adminUserId = $('.appointment .showTable .pickBookingPerson option:selected').val();//管理员ID
	// adminUserId = parseInt(adminUserId);

	var robotId = $('.appointment .showTable .pickRobot option:selected').val();//机器人ID
	robotId = parseInt(robotId);

	var startTime = $('.appointment .showTable .sel .start-time').val();//开始时间
	var endTime = $('.appointment .showTable .sel .end-time').val();;//结束时间

	var status = $('.appointment .showTable .pickState option:selected').val();//状态
	// status = parseInt(status);

	var data = {
		adminUserId: adminUserId,
		robotId: robotId,
		startTime: startTime,
		endTime: endTime,
		status: status
	}
	console.log('查询',data);
	
	initAppointmentShow(data);
});

//预约管理，展示表格界面，点击编辑按钮，展示表格隐藏，编辑表格显示
$('.appointment .showTable .title > button.edit').click(function () {
	$('.showTable').hide();
	$('.editTable').show();

	// var adminUserId = 17 ;//管理员ID
	// var robotId = 1 ;//机器人ID
	// var startTime ='2018-08-05 14:29';//开始时间
	// var endTime ='2018-10-05 14:29' ;//结束时间
	// var status = 1;//状态

	var adminUserId = $('.appointment .showTable .pickBookingPerson option:selected').val();//管理员ID
	// adminUserId = parseInt(adminUserId);

	var robotId = $('.appointment .showTable .pickRobot option:selected').val();//机器人ID
	robotId = parseInt(robotId);

	var startTime = $('.appointment .showTable .sel .start-time').val();//开始时间
	var endTime = $('.appointment .showTable .sel .end-time').val();;//结束时间

	var status = $('.appointment .showTable .pickState option:selected').val();//状态
	// status = parseInt(status);

	var data = {
		adminUserId: adminUserId,
		robotId: robotId,
		startTime: startTime,
		endTime: endTime,
		status: status
	}

	initAppointmentEdit(data);
});

//预约管理，展示表格界面，点击新增按钮，调用新增预约的弹窗
$('.appointment .title > button.new').click(function () {

	var adminUserId = $('.appointment .showTable .pickBookingPerson option:selected').val();//管理员ID
	

	if (adminUserId == null || adminUserId == '' || adminUserId == undefined) {
		alert('请选择预约人');
		return;
	}
	// adminUserId = parseInt(adminUserId);

	$('.appointmentModal').modal('show');

});


//预约管理，新增弹窗，确定按钮事件
$('.appointmentModal button.confirm').click(function () {

	$('.appointmentModal').modal('hide');

	//提交新的预约
	// var adminUserId = 17;//管理员ID

	var adminUserId = $('.appointment .showTable .pickBookingPerson option:selected').val();//管理员ID
	

	if (adminUserId == null || adminUserId == '' || adminUserId == undefined) {
		alert('请选择预约人');
		return;
	}
	// adminUserId = parseInt(adminUserId);

	var robotId = $('.appointmentModal .pickRobot option:selected').val();//预约智能机器人ID
	robotId = parseInt(robotId);


	var startTime = $('.appointmentModal .modal-body .form-group #start-yuyue').val();//预约开始时间

	var reservationAbstract = $('.appointmentModal #zhaiyao').val();//预约摘要

	var reservationNum = $('.appointmentModal #yuyue-Pnum').val();//访客数
	reservationNum = parseInt(reservationNum);

	var arr = [];
	$('.appointmentModal .modal-body .pickDepartment li.selected').each(function (i) {
		var index = $(this).attr('data-original-index');
		arr[i] = index;
	});

	if (arr.length < 1) {
		alert('请选择行业');
		return;
	}

	var industryIds = []; //行业ID组
	for (var i = 0; i < arr.length; i++) {

		var a = arr[i];

		var obj = industry_list[a];

		industryIds.push(obj.id);

	}

	var data = {
		adminUserId: adminUserId,
		robotId: robotId,
		startTime: startTime,
		reservationAbstract: reservationAbstract,
		reservationNum: reservationNum,
		industryIds: industryIds
	}

	addReservation(data);

	//预约成功后 获取所有的预约信息 刷新页面

});



//预约管理，编辑表格界面，点击保存按钮，编辑表格隐藏，展示表格显示
$('.appointment .title > button.editsave').click(function () {

	//获取勾选行的索引
	var checkedArr = new Array();

	$('.appointment .tableAppointmentEdit tbody tr td:nth-child(8) input:checked').each(function (i) {

		var index = $(this).parents('tr').index();
		checkedArr[i] = index;
	});

	console.log('checkedArr', checkedArr);

	if (checkedArr.length < 1) {
		alert('请选择需要保存的预约');
		return;
	}

	var ary_save = [];
	for (var i = 0; i < checkedArr.length; i++) {

		var a = checkedArr[i];
		var obj = order_list[a];
		obj.robotId = obj.robotId;//目前只有一个机器人  $('.appointment .showTable .pickRobot option:selected').val();

		let startTime = $('.bianji_start').eq(a).val();

		obj.startTime = startTime;

		let adminUserId = $('.bianji_admin option:selected').eq(a).val();

		obj.adminUserId = parseInt(adminUserId);

		ary_save.push(obj);
	}


	ajaxAppointmentSave(ary_save);

	$('.editTable').hide();
	$('.showTable').show();


});

//预约管理，编辑表格界面，点击取消按钮，编辑表格隐藏，展示表格显示
$('.appointment .title > button.cancel').click(function () {
	$('.editTable').hide();
	$('.showTable').show();
});

//预约管理，编辑表格界面，删除列按钮点击事件
$('.appointment .editTable').on('click', 'td:nth-child(7)', function () {

	var deleteRow = $(this).parent().index();
	alert('删除的数据:' + deleteRow);

	//将被删除的数据
	var obj = order_list[deleteRow];

	// console.log('将被删除的数据',obj);


	var reservationId = obj.reservationId;//预约ID

	// var adminUserId = 17;//当前管理员ID

	var adminUserId = $('.appointment .showTable .pickBookingPerson option:selected').val();//管理员ID
	

	if (adminUserId == null || adminUserId == '' || adminUserId == undefined) {
		alert('请选择预约人');
		return;
	}
	adminUserId = parseInt(adminUserId);


	var cause = '';


	var objData = {
		reservationId: reservationId,
		adminUserId: adminUserId,
		cause: cause
	}

	cancelReservation(objData);

	//删除之后 刷新页面

});

//预约管理，编辑表格界面，删除按钮点击事件
$('.appointment .editTable .button > button').click(function () {
	alert('删除所有选定行的数据');
});

























